package com.wn.study.view

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.paging.LoadState
import androidx.paging.LoadStateAdapter
import com.wn.common.widget.BindingViewHolder
import com.wn.study.databinding.FooterItemBinding
import dagger.hilt.android.scopes.ActivityScoped
import javax.inject.Inject

@ActivityScoped
class FooterAdapter @Inject constructor(private val retry: () -> Unit) :
  LoadStateAdapter<BindingViewHolder>() {
  override fun onCreateViewHolder(parent: ViewGroup, loadState: LoadState): BindingViewHolder {
    val binding = FooterItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
    return BindingViewHolder(binding)
  }

  // 默认 加载时 显示 整个布局  这里是 布局内组件 区分显示
  override fun onBindViewHolder(holder: BindingViewHolder, loadState: LoadState) {
    val binding = holder.binding as FooterItemBinding
    binding.proFooter.isVisible = loadState is LoadState.Loading
    binding.retryButton.isVisible = loadState is LoadState.Error
    // 重试
    binding.retryButton.setOnClickListener {
      retry()
    }
//    水平居中   针对瀑布流
/*      val params = holder.itemView.layoutParams
      if (params is StaggeredGridLayoutManager.LayoutParams) params.isFullSpan = true*/
  }
}